package com.xayy.health.mgt.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.EnumUtil;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.xayy.health.core.enums.ScoreRuleEnum;
import com.xayy.health.mgt.model.FractionIntervalScore;
import com.xayy.health.mgt.model.vo.FractionIntervalScoreVo;
import com.xayy.health.mgt.service.FractionIntervalScoreService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Nie
 * @version V1.0
 * @date 2023/4/11 13:59
 */
@Service
public class FractionIntervalScoreServiceImpl extends BaseServiceImpl<FractionIntervalScore>
        implements FractionIntervalScoreService {

    /**
     * 根据类型获取打分等级信息
     *
     * @param type 类型
     * @return 打分等级信息集合
     */
    @Override
    public CheckResult<FractionIntervalScoreVo> selectByType(Integer type) {
        List<FractionIntervalScore> list = select(FractionIntervalScore.builder().fractionType(type).build());
        if (list.isEmpty()) {
            return CheckResult.fail("未查询到相关信息");
        }
        FractionIntervalScoreVo intervalScoreVo = new FractionIntervalScoreVo();
        intervalScoreVo.setFractionType(list.get(0).getFractionType());
        intervalScoreVo.setIntervalList(list);
        return CheckResult.success(intervalScoreVo);
    }

    /**
     * 获取下拉列表信息
     *
     * @return 结果日志
     */
    @Override
    public CheckResult<?> spinner() {
        List<Map<String, Object>> list = EnumUtil.toList(ScoreRuleEnum.class);
        List<FractionIntervalScore> all = selectAll();
        Set<Integer> set = all.stream().map(FractionIntervalScore::getFractionType).collect(Collectors.toSet());
        list.removeIf(x -> set.contains(x.get("code")));
        List<Map<String, Object>> list2 = EnumUtil.toList(ScoreRuleEnum.class);
        list2.removeIf(x -> x.get("code").equals(3));
        return CheckResult
                .success(MapUtil.builder().put("fractionType", list2).put("notEnterFractionType", list).build());
    }
}
